/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GameFrameworkJavaFX;


import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;


/**
 *
 * @author anc
 */
public class Gui extends Application {
    
    private static Gui gui;
    private Pane root;
    private Button startButton;
    private Timeline worldTime;
    private Button exitButton;
    
    MediaPlayer musicPlayer;
    MediaPlayer musicPlayer2;
    Label scoreLabel;
    Label lifeLabel;
      


  @Override
    public void start(Stage primaryStage) {
        final int windowWidth = 1366;
        final int windowHeight = 768;
        gui = this;
        
        root = new Pane();
        root.setStyle("-fx-background-color: black;");
    
        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add("style.css");
        setBackground("pane_title");

        scene.setOnKeyPressed((KeyEvent event) -> {
            //System.out.println("KeyEvent character "+event.getCode());
            Game.getInstance().keyPressed(event.getCode());
        });
        
        scene.setOnKeyReleased((KeyEvent event) -> {
            //System.out.println("KeyEvent character "+event.getCode());
            Game.getInstance().keyReleased(event.getCode());
        });
        
        scene.setOnMousePressed((MouseEvent event) -> {
            Game.getInstance().mousePressed(event.getButton() == MouseButton.PRIMARY, (int)event.getX(), (int)event.getY());
        });
        
        //Make fullscreen...
        Screen screen = Screen.getPrimary();
        javafx.geometry.Rectangle2D bounds = screen.getVisualBounds();
        primaryStage.setX(bounds.getMinX());
        primaryStage.setY(bounds.getMinY());
        primaryStage.setWidth(windowWidth);
        primaryStage.setHeight(windowHeight);
        primaryStage.setResizable(false);

        primaryStage.setTitle("Krakout - the remake");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        startButton = new Button();
        startButton.setText("Click to Start Game");
        startButton.setOnAction((ActionEvent event) -> {
            System.out.println("The game was started...!");
            Game.getInstance().setGameState(GameState.Playing);
            
        });
        startButton.setLayoutX(windowWidth/2);
        startButton.setLayoutY(100);
        root.getChildren().add(startButton);
        
        exitButton  = new Button();
        exitButton.setText("Click to Exit game");
        exitButton.setOnAction((ActionEvent event) -> {
            System.out.println("The game was exit...!");
            Game.getInstance().setGameState(GameState.Exit);
            
        });
        exitButton.setLayoutX((windowWidth/2)+3);
        exitButton.setLayoutY(140);
        root.getChildren().add(exitButton);
        
        scoreLabel = new Label("Score: 0");
        scoreLabel.setFont(new Font(STYLESHEET_MODENA, 20));
        scoreLabel.setTextFill(Color.WHITE);
        scoreLabel.setLayoutX((100));
        scoreLabel.setVisible(false);
        root.getChildren().add(scoreLabel);
        
        lifeLabel = new Label("Life: 3");
        lifeLabel.setFont(new Font(STYLESHEET_MODENA, 20));
        lifeLabel.setTextFill(Color.WHITE);
        lifeLabel.setLayoutX((1266));
        lifeLabel.setVisible(false);
        root.getChildren().add(lifeLabel);
        
        
       
        Game.createGameAndSetGui(this);
        
        playMusic();
        
    }
    public void setBackground(String paneId){
        root.setId(paneId);
    }
    public void updateScore(int newScore){
        scoreLabel.setText("Score: "+Integer.toString(newScore));
    }
    
    public void updateLife(int newScore){
        lifeLabel.setText("Life: "+Integer.toString(newScore));
    }

    public void startWorldTime(){
        worldTime = new Timeline(new KeyFrame(Duration.millis(25), (ActionEvent event) -> {
            Game.getInstance().timeTick();
        }));      
        worldTime.setCycleCount(Timeline.INDEFINITE);
        worldTime.play();        
    }
    
    public void stopWorldTime() {
        Game.getInstance().timeTick();
        worldTime.stop();
    }
    
    
    public void updateFromState(GameState gameState){
        switch (gameState){
            case TitleScreen:
                
                setBackground("pane_title");
                //resumeButton.setVisible(true);
                startButton.setVisible(true);
                exitButton.setVisible(true);
                musicPlayer.play();
                break;
                
            case Playing:
               
                Game.getInstance().level1 = true;
                setBackground("pane_playing");
                Game.getInstance().createActors();
                startWorldTime();
                startButton.setVisible(false);
                exitButton.setVisible(false);
                musicPlayer.stop();
                playMusic2();
                musicPlayer2.play();
                scoreLabel.setVisible(true);
                lifeLabel.setVisible(true);
                break;
                
            case GameOver:
                stopWorldTime();
                setBackground("pane_gameover");
                scoreLabel.setVisible(false);
                startButton.setVisible(true);
                exitButton.setVisible(true);
                musicPlayer2.stop();
                lifeLabel.setVisible(false);
                break;  
            
            case GameComplete:
                stopWorldTime();
                setBackground("pane_game_complete");
                scoreLabel.setVisible(false);
                startButton.setVisible(false);
                exitButton.setVisible(true);
                musicPlayer2.stop();
                lifeLabel.setVisible(false);
                 break;
                
                
            case Exit:
                System.exit(0);
                break;
              
        }
      }
      
    public void ExitGame(){
        System.exit(0);
    }
    
    public void showMenuButtonWithText(String textOnButton){
        startButton.setText(textOnButton);
        startButton.setVisible(true);
        exitButton.setText(textOnButton);
        exitButton.setVisible(true);
        
        
    }
    
    public void addImageView(ImageView imageView){
        root.getChildren().add(imageView);
    }

    public void removeImageView(ImageView imageView){
        root.getChildren().remove(imageView);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    public static Gui getInstance(){ 
        return gui;
    }
    
    public int getHeight(){
        return (int)root.getHeight();
    }
    
    public int getWidth(){
        return (int)root.getWidth();
    }
    
    public void playMusic(){
        Media audioFile = new Media( getClass().getResource("/resources/chipN00bz-M3nu.mp3").toString() );
        try {
            musicPlayer = new MediaPlayer(audioFile);
            musicPlayer.play();
            musicPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        }
        
        catch (Exception e){
            System.out.println( e.getMessage() );
            System.exit(0);
        }        
    } 
    
    public void playMusic2(){
        Media audioFile = new Media( getClass().getResource("/resources/chipN00bz-inGame.mp3").toString() );
        try {
            musicPlayer2 = new MediaPlayer(audioFile);
            musicPlayer2.play();
            musicPlayer2.setCycleCount(MediaPlayer.INDEFINITE);
        }
        
        catch (Exception e){
            System.out.println( e.getMessage() );
            System.exit(0);
        }        
    } 
    
    
    
}